/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.com.hydroPK.model.skills.crafting;

import net.com.hydroPK.*;
import net.com.hydroPK.model.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CraftingTimer {
    private int counter = 0;
    private Timer timer;

    /**
     * Handle the timering of the crafting things.
     *
     * @param p The caster.
     */
    public CraftingTimer(final Player p, final int item, final int amount, final double xp, final int times, final int delete, final int delamount, final int emote, final boolean isNeedThread) {
        //Need correct delays to glassblowing, and leathercrafting
        timer = new Timer(2000, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (p.disconnected[0]) {
                    timer.stop();
                }
                if (p.isInterruptable) {
                    if (isNeedThread) {
                        p.threadLeft--;
                        if (p.threadLeft <= 0) {
                            Engine.playerItems.deleteItem(p, 1734, Engine.playerItems.getItemSlot(p, 1734), 1);
                            if (Engine.playerItems.invItemCount(p, 1734) > 0) {
                                p.threadLeft = 5;
                            }
                        }
                    }
                    p.appendExperience2(xp, 12);
                    Engine.playerItems.deleteItem(p, delete, Engine.playerItems.getItemSlot(p, delete), delamount);
                    Engine.playerItems.addItem(p, item, amount);
                    counter++;
                    if (Engine.playerItems.invItemCount(p, delete) < delamount && counter < times) {
                        counter = times;
                    }
                } else {
                    counter = times;
                }
                if (counter == times) {
                    timer.stop();
                    p.isBussy = false;
                    p.isInterruptable = false;
                } else {
                    p.requestAnim(emote, 0);
                }
            }
        });
        timer.setRepeats(true);
        timer.start();
    }
}
